package thesis.filesharing.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import thesis.filesharing.common.exception.system.AppSystemException;
import thesis.filesharing.dao.SemesterDAO;
import thesis.filesharing.dao.util.EMF;
import thesis.filesharing.model.Semester;

@Repository
public class SemesterDAOImpl implements SemesterDAO {

	@SuppressWarnings("unchecked")
	@Override
	public List<Semester> listSemester() {
		EntityManager em = null;
		try {
			em = EMF.get().createEntityManager();
			String jpaql = "SELECT s FROM Semester s";
			Query query = em.createQuery(jpaql);
			return (List<Semester>) query.getResultList();
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new AppSystemException(ex);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public void updateCurrentSemester(Integer semesterName) {
		EntityManager em = null;
		EntityTransaction transaction = null;
		
		try {
			em = EMF.get().createEntityManager();
			transaction = em.getTransaction();
			transaction.begin();
			
			Query query = null;
			List<Semester> semesters = null;
			Semester semester;
			
			// Check if semesterName is already existed in datastore
			String jpaql = "SELECT s FROM Semester s WHERE s.semesterName = :semesterName";
			query = em.createQuery(jpaql);
			query.setParameter("semesterName", semesterName);
			semesters = (List<Semester>) query.getResultList();
			if (semesters != null && !semesters.isEmpty()) {
				semester = semesters.get(0);
				if (semester.getCurrent()) {
					transaction.commit();
					return;
				}
			} else {
				semester = new Semester(semesterName, false);
				em.persist(semester);
				em.flush();
			}
			
			// Update every semester whose current field is set to true
			String jpaql1 = "SELECT s FROM Semester s WHERE s.current = true";
			query = em.createQuery(jpaql1);
			semesters = (List<Semester>) query.getResultList();
			for (Semester s : semesters) {
				s.setCurrent(false);
				em.merge(s);
			}
			
			semester.setCurrent(true);
			em.merge(semester);
			
			transaction.commit();
		} catch (Exception ex) {
			if (transaction != null && transaction.isActive()) {
				transaction.rollback();
			}
			ex.printStackTrace();
			throw new AppSystemException(ex);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public Semester getCurrentSemester() {
		EntityManager em = null;
		try {
			em = EMF.get().createEntityManager();
			String jpaql = "SELECT s FROM Semester s WHERE s.current = true";
			Query query = em.createQuery(jpaql);
			List<Semester> semesters = (List<Semester>) query.getResultList();
			if (semesters != null && !semesters.isEmpty()) {
				return semesters.get(0);
			}
			return null;
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new AppSystemException(ex);
		} finally {
			em.close();
		}
	}
}
